<template>
  <h1>员工列表练习：</h1>
  <el-form label-width="80" style="width: 600px;margin: 0 auto;">
    <el-form-item label="姓名" >
      <el-input placeholder="请输入用户名" v-model="user.name"></el-input>
    </el-form-item>

    <el-form-item label="工资" >
      <el-input placeholder="请输入工资" v-model="user.salary"></el-input>
    </el-form-item>

    <el-form-item label="岗位" >
      <el-input placeholder="请输入岗位" v-model="user.job"></el-input>
    </el-form-item>

    <el-form-item>
       <el-button type="success" @click="show()">点击提交</el-button>
    </el-form-item>

  </el-form>

  <el-table :data="arr" style="width: 600px;margin:0 auto;">
    <el-table-column type="index" label="编号" width="80" ></el-table-column>
    <el-table-column prop="name" label="姓名" ></el-table-column>
    <el-table-column prop="salary" label="工资" ></el-table-column>
    <el-table-column prop="job" label="岗位" ></el-table-column>

    <!-- 自定义列模版   -->
    <el-table-column label="操作" >
      <!--scope保存了当前行数据的索引$index(从0开始)与当前行的完整数据row -->
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>


</template>


<script setup>

import {ref} from "vue";

const user =ref({name:'', salary:'', job:''});

const arr = ref([]);

const show = ()=>{
  //在控制台打印
  // console.log(user.value);
  // 将新员工信息添加到数组中
  arr.value.push(user.value);

}

// 删除数据
function del(index, user) {
   if (confirm('你确认要删除' + user.name + '吗？')) {
    /*删除数据，从当前行开始，删除一条数据*/
    arr.value.splice(index, 1);
  }
}

</script>


<style scoped>

</style>